<?php
class ImportController extends Controller {
	private $_fileDir = './database/';
	public function init(){
		parent::init();
		// echo Yii::app()->controller->id.'/'.Yii::app()->controller->action->id;
		//$files = glob($this->_fileDir.$this->id.'/'.$this->action->id.'/*.txt');
		// print_r($files);
		//$sqls = file( $this->_fileDir.$this->id.'/'.$this->action->id.'/');
		//	foreach ($sqls as $key => $sql) {
		//		;
		//	}

	}
	public function actionContact() {
		$dir = Yii::app()->basePath. strtolower( $this->_fileDir.$this->id.'/'.$this->action->id.'/*.txt');
		$files = glob ('./database/import/contact/*.txt');
		// print_r($sqls);
		foreach($files as $key=>$filename){
			$sqls = file($filename);
			$sqls = array_slice($sqls, 0, 10);
			// print_r($sqls);
			foreach ($sqls as $sql) {
				preg_match('/insert\s*?into.*?values\s*?\(.*?\)/i', $sql, $matches);
				// echo '<BR>';
				//var_dump(empty($matches));
				if (empty($matches)) continue;
				
				$text = preg_replace('/insert\s*?into/i', 'replace into', $matches[0]);
				
				preg_match_all('/\'([^,]+?\'[^,]+?)\',/i', $text, $matches);
				
				echo $text;
				print_r($matches);
				
				echo preg_replace('/\'([^,]+?\'[^,]+?)\',/i', addslashes("$1"), $text);
				
				echo "<BR>$text";break;
				//print_r($matches);
				//$text = $matches[0][0];
				$execute = Yii::app()->db->createCommand($text)->execute();
				
				if (!$execute) {
					echo '<BR>'.$text;
				}
			}
			break;
		}
		foreach ($sqls as $key => $sql) {
			;
		}
	}
	public function actionCustomer() {

	}
}
?>